Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient

Public Partial Class TheatreStaff_InsertMovie
	Inherits System.Web.UI.Page
	'protected void Page_Load(object sender, EventArgs e)
	'{
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		srcMovie.SelectParameters("username").DefaultValue = Page.User.Identity.Name
		srcMoviegrd.SelectParameters("username").DefaultValue = Page.User.Identity.Name

		Dim connectionString As String
		connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

		Dim con As New SqlConnection(connectionString)
		Dim cmd As New SqlCommand("SELECT TheatreId from TheatreStaff WHERE TheatreStaffUsername=@username", con)

		cmd.Parameters.AddWithValue("@username", Page.User.Identity.Name)

		Using con
			con.Open()
			Dim TheatreID As Object = cmd.ExecuteScalar()
			If TheatreID IsNot Nothing Then
				lblId.Text = TheatreID.ToString()
			End If
		End Using


		Dim con1 As New SqlConnection(connectionString)
		Dim cmd1 As New SqlCommand("SELECT Theatrename from Theatre WHERE Theatreid=@theatreid", con1)

		cmd1.Parameters.AddWithValue("@theatreid", lblId.Text)


		Using con1
			con1.Open()
			Dim Theatrename As Object = cmd1.ExecuteScalar()

			If Theatrename IsNot Nothing Then

				Label2.Text = Theatrename.ToString()
			End If
		End Using

	End Sub
    '}
    Protected Sub Clear(ByVal sender As Object, ByVal e As EventArgs)
        Label15.Text = String.Empty
        Label16.Text = String.Empty
    End Sub

    Protected Sub Cancel(ByVal sender As Object, ByVal e As EventArgs)

        txtMovieTitleI.Text = String.Empty
        txtMovieDirectorI.Text = String.Empty
        chkShowingI.Checked = False
        txtMovieDescriptionI.Text = String.Empty
        txtMovieDurationI.Text = String.Empty
        txtimagefnameI.Text = String.Empty
        txtRatingI.Text = String.Empty
        txtTrailersI.Text = String.Empty
        txtTheatreDateReleasedI.Text = String.Empty
        txtMaincastI.Text = String.Empty
        Label15.Text = ""
        Label16.Text = ""
        ddlCategoryI.SelectedIndex = -1
        'grdMovie.DataBind();
    End Sub
    Protected Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs)
        srcMovie.InsertParameters("TheatreId").DefaultValue = lblId.Text
        'grdMovie.DataBind();
        Label15.Text = ""
        Label16.Text = ""
    End Sub

    Protected Sub dtlSchedule_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
        'grdMovie.DataBind();
        Label15.Text = ""
        Label16.Text = ""
    End Sub

    Protected Sub InsertMovie_Click(ByVal sender As Object, ByVal e As EventArgs)


        Dim connectionString As String
        connectionString = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As New SqlConnection(connectionString)
        Dim con1 As New SqlConnection(connectionString)
        Dim cmd1 As New SqlCommand("INSERT Movie(CategoryId,MovieTitle,MovieDirector,Showing,MovieDescription,TheatreId,MovieDuration,imagefname,Rating,Trailers,TheatreDateReleased,maincast) values (@CategoryId,@MovieTitle,@MovieDirector,@Showing,@MovieDescription,@TheatreId,@MovieDuration,@imagefname,@Rating,@Trailers,@TheatreDateReleased,@maincast)", con1)
        Dim cmd As New SqlCommand("SELECT MovieTitle,MovieDirector from Movie WHERE TheatreId=@TheatreId AND MovieTitle=@MovieTitle AND MovieDirector=@MovieDirector ", con)
        Dim ParamString As String = lblId.Text
        Dim Showing As [Boolean]
        Dim RowCount As Int32
        Dim HasMovies As [Boolean]

        cmd.Parameters.AddWithValue("@TheatreId", ParamString)
        cmd.Parameters.AddWithValue("@MovieDirector", txtMovieDirectorI.Text)
        cmd.Parameters.AddWithValue("@MovieTitle", txtMovieTitleI.Text)

        Using con
            con.Open()
            Dim Reader As SqlDataReader = cmd.ExecuteReader()

            If Reader.HasRows Then
                Label16.Text = ""
                Label15.Text = "The movie " + txtMovieTitleI.Text + " already exist in the database!"

                'lblCheck.Text = string.Empty;
                'ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "Focus", "<script language='JavaScript' 'showInsert();'/>");


                HasMovies = True
            Else

                HasMovies = False
            End If
        End Using
        Using con1
            con1.Open()
            If HasMovies = False Then
                If chkShowingI.Checked Then

                    Showing = True
                Else
                    Showing = False
                End If

                cmd1.Parameters.AddWithValue("@CategoryId", ddlCategoryI.SelectedValue)
                cmd1.Parameters.AddWithValue("@MovieTitle", txtMovieTitleI.Text)
                cmd1.Parameters.AddWithValue("@MovieDirector", txtMovieDirectorI.Text)
                cmd1.Parameters.AddWithValue("@Showing", Showing)
                cmd1.Parameters.AddWithValue("@MovieDescription", txtMovieDescriptionI.Text)
                cmd1.Parameters.AddWithValue("@TheatreId", lblId.Text)
                cmd1.Parameters.AddWithValue("@MovieDuration", txtMovieDurationI.Text)
                cmd1.Parameters.AddWithValue("@imagefname", txtimagefnameI.Text)
                cmd1.Parameters.AddWithValue("@Rating", txtRatingI.Text)
                cmd1.Parameters.AddWithValue("@Trailers", txtTrailersI.Text)
                cmd1.Parameters.AddWithValue("@TheatreDateReleased", DateTime.Parse(txtTheatreDateReleasedI.Text))
                cmd1.Parameters.AddWithValue("@maincast", txtMaincastI.Text)
                RowCount = cmd1.ExecuteNonQuery()
                ddlCategoryI.SelectedIndex = -1
                txtMovieTitleI.Text = String.Empty
                txtMovieDirectorI.Text = String.Empty
                chkShowingI.Checked = False
                txtMovieDescriptionI.Text = String.Empty
                txtMovieDurationI.Text = String.Empty
                txtimagefnameI.Text = String.Empty
                txtRatingI.Text = String.Empty
                txtTrailersI.Text = String.Empty
                txtTheatreDateReleasedI.Text = String.Empty
                txtMaincastI.Text = String.Empty

                'if (RowCount > 0)
                '{
                Label15.Text = ""
                'lblCorrect.Visible = true;


                '}
                Label16.Text = "The Movie " + txtMovieTitleI.Text + " has been added sucessfully!"

            End If
        End Using
        'grdMovie.DataBind();
    End Sub
    Private Sub Page_Load()
        If Not Page.IsPostBack Then
            'grdMovie.SelectedIndex = 0;
            Label15.Text = String.Empty
        End If
        Label16.Text = String.Empty
    End Sub
End Class
